<?php
class ShareCommentAddAction extends FrontendAction
{
	public function __construct()
	{
		parent::__construct();
		$this->responseType = 'json';
	}
	
	public function execute()
	{
		$this->code 	= 0;
		$content 		= $this->context->post('content');
		$share_id 		= intval($this->context->post('share_id'));
		if (!$share_id > 0){
			$this->code 	= 101;
			$this->message = '分享不存在';
			return;
		}
		if (!$content){
			$this->code 	= 101;
			$this->message = '点评内容不能为空';
			return;
		}
		$shareVtui = true;//在此去服务端验证是否有这个分享
		
		if (!$shareVtui){
			$this->code 	= 101;
			$this->message = '分享不存在';
			return;
		}
		
		if ($shareVtui['user_id'] == $this->data['user']['id']){
			$this->code 	= 101;
			$this->message = '不能自己点评自己的分享';
			return;
		}
		
		$shareCommentService	= new BaseDbService('share_comment');
		$map = array('share_id'=>$share_id, 'user_id'=>$this->data['user']['id'], 'content'=>$content);
		$id	= $shareCommentService->add($map);
		if ($id){
			//更新分享表
			$shareService	= new BaseDbService('share');
			$shareInfo		= $shareService->get($share_id);
			if ($shareInfo){
				$shareService->update($share_id, array('comment_num'=>'expression:comment_num+1'));
			}else{
				$shareService->add(array('id'=>$share_id, 'comment_num'=>1));
			}
			//同时加入动态
			$weiboService	= new BaseDbService('weibo');
			$map = array('share_id'=>$share_id, 'user_id'=>$this->data['user']['id'], 'content'=>$content);
			$weiboService->add($map);
			
			$this->message	= '';
			$this->code 	= 100;//成功
			$created = Util::getBeforeTimelag(time());
			$this->data['home'] = C('LtUrl')->generate('User','Index', array('uid'=>$this->data['user']['id']));
			$this->data['logo'] = $this->data['user']['logo'];
			$this->data['username'] = $this->data['user']['username'];
			$this->data['content'] = $content;
			$this->data['created'] = $created;
		}else{
			$this->code 	= 101;//失败
			$this->message	= '哎呀，不小心被雷劈了下真倒霉，再试一下';
		}
		
	}
}
